#include "conversationmodel.h"
#include "appchat/accountmanager.h"

#include <QDebug>
#include <QDateTime>

ConversationModel::ConversationModel(QObject *parent)
    : QAbstractListModel(parent)
{
}

void ConversationModel::addConversation(Converse::AbstractMessage* conversation)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_listConversation.push_back(conversation);
    endInsertRows();
}

void ConversationModel::editConversation(Converse::AbstractMessage* conversation)
{
    Q_UNUSED(conversation);
    beginResetModel();
    endResetModel();
}

void ConversationModel::insertConversation(const int pos, Converse::AbstractMessage* conversation)
{
    beginInsertRows(QModelIndex(), pos, pos);
    if (m_listConversation.length() == 0) {
        m_listIsCombinedMessage.insert(pos, true);
    } else {
        if (conversation->senderId() == m_listConversation.first()->senderId()) {
            m_listIsCombinedMessage[0] = false;
        }
        m_listIsCombinedMessage.insert(pos, true);
    }
    m_listConversation.insert(pos, conversation);
    endInsertRows();

    beginResetModel();
    endResetModel();
}


int ConversationModel::rowCount(const QModelIndex & parent) const {
    Q_UNUSED(parent);
    return m_listConversation.count();
}

QVariant ConversationModel::data(const QModelIndex & index, int role) const {
    if (index.row() < 0 || index.row() >= m_listConversation.count())
        return QVariant();

    const Converse::AbstractMessage* conversation = m_listConversation[index.row()];
    const bool isCombinedMessage = m_listIsCombinedMessage[index.row()];

    if (role == IdRole)
        return QVariant::fromValue<qulonglong>(conversation->id());
    else if (role == SendingIdRole)
        return QVariant::fromValue<qulonglong>(conversation->sendingId());
    else if (role == SenderIdRole)
        return QVariant::fromValue<qulonglong>(conversation->senderId());
    else if (role == ReceiverIdRole)
        return QVariant::fromValue<qulonglong>(conversation->receiverId());
    else if (role == ReceiveTimeRole)
        return QVariant::fromValue<qulonglong>(conversation->receivedTime());
    else if (role == CreatedTimeRole) {
        QDateTime dateTime;
        dateTime.setMSecsSinceEpoch(conversation->createdTime() * 1000);
        QString time = dateTime.toString("hh:mm AP");
        return time;
    } else if (role == TypeRole)
        return conversation->type();
    else if (role == MessageRole)
        return conversation->message();
    else if (role == ExtraRole)
        return conversation->extra();
    else if (role == DeliveredMessageRole) {
        if (conversation->senderId() == AccountManager::instance()->getUserId()) {
            if (conversation->id() == -1) {
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    } else if (role == DateTimeCombinationRole) {
        QDateTime dateTime;
        dateTime.setMSecsSinceEpoch(conversation->createdTime() * 1000);
        return dateTime.toString("MMM-dd-yy");
    } else if (role == IsCombinedMessageRole) {
        return isCombinedMessage;
    }

    return QVariant();
}


QHash<int, QByteArray> ConversationModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[IdRole] = "messageId";
    roles[SendingIdRole] = "sendingId";
    roles[SenderIdRole] = "senderId";
    roles[ReceiverIdRole] = "receiverId";
    roles[ReceiveTimeRole] = "receiveTime";
    roles[CreatedTimeRole] = "createdTime";
    roles[TypeRole] = "messageType";
    roles[MessageRole] = "message";
    roles[ExtraRole] = "extra";
    roles[DeliveredMessageRole] = "deliveredMessage";
    roles[DateTimeCombinationRole] = "datetimeCombination";
    roles[IsCombinedMessageRole] = "isCombinedMessage";

    return roles;
}

void ConversationModel::setListConversation(const QList < Converse::AbstractMessage *> _listConversation)
{
    beginResetModel();
    m_listConversation = _listConversation;
    endResetModel();
}

void ConversationModel::clear()
{
    beginResetModel();
    m_listConversation.clear();
    m_listIsCombinedMessage.clear();
    endResetModel();

}
